﻿/* 
 * Copyright (c) 2009 Intel Corporation
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -- Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * -- Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * -- Neither the name of the Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE INTEL OR ITS
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * Contributor: Nathan Letwory
 */

using System;

namespace VerseSharp
{
    public delegate void ObjectTransformPosReal32Callback(uint nodeID, float timeS, float timeF, byte mask, float[] pos, float[] speed, float[] accel, float[] dragnormal, float drag);

    public class ObjectTransformPosReal32Command : IVerseCommand
    {
        public CommandID ID { get { return CommandID.ObjectTransformPosReal32; } }
        public uint NodeID;
		public float TimeS;
		public float TimeF;
		public byte Mask;
		public float[] Pos = null;
		public float[] Speed = null;
		public float[] Accel = null;
		public float[] DragNormal = null;
		public float Drag = float.NaN;

        public void Deserialize(byte[] source, ref int pos)
        {
			float x, y, z;
            NodeID = DataConverter.BigEndian.GetUInt32(source, pos);
            pos += 4;
			TimeS = DataConverter.BigEndian.GetFloat(source, pos);
			pos += 4;
			TimeF = DataConverter.BigEndian.GetFloat(source, pos);
			pos += 4;

			Mask = (byte)source[pos++];

			x = DataConverter.BigEndian.GetFloat(source, pos);
			pos += 4;
			y = DataConverter.BigEndian.GetFloat(source, pos);
			pos += 4;
			z = DataConverter.BigEndian.GetFloat(source, pos);
			pos += 4;

			SetPos(x, y, z);

			if ((Mask & 1) == 1)
			{
				x = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				y = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				z = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				SetSpeed(x, y, z);
			}
			if ((Mask & 2) == 2)
			{
				x = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				y = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				z = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				SetAccel(x, y, z);
			}
			if ((Mask & 4) == 4)
			{
				x = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				y = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				z = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
				SetDragNormal(x, y, z);
			}
			if ((Mask & 8) == 8)
			{
				Drag = DataConverter.BigEndian.GetFloat(source, pos);
				pos += 4;
			}
        }

		public void SetPos(float x, float y, float z)
		{
			if (Pos == null)
			{
				Pos = new float[3];
			}
			Pos[0] = x;
			Pos[1] = y;
			Pos[2] = z;
		}

		public void SetSpeed(float x, float y, float z)
		{
			if (Speed == null)
			{
				Speed = new float[3];
			}
			Speed[0] = x;
			Speed[1] = y;
			Speed[2] = z;
		}

		public void SetAccel(float x, float y, float z)
		{
			if (Accel == null)
			{
				Accel = new float[3];
			}
			Accel[0] = x;
			Accel[1] = y;
			Accel[2] = z;
		}

		public void SetDragNormal(float x, float y, float z)
		{
			if (DragNormal == null)
			{
				DragNormal = new float[3];
			}
			DragNormal[0] = x;
			DragNormal[1] = y;
			DragNormal[2] = z;
		}

        public void Serialize(byte[] destination, ref int pos)
        {
            DataConverter.BigEndian.PutBytes(destination, pos, NodeID);
            pos += 4;
			DataConverter.BigEndian.PutBytes(destination, pos, TimeS);
			pos += 4;
			DataConverter.BigEndian.PutBytes(destination, pos, TimeF);
			pos += 4;

			int maskPos = pos;
			pos++;

			/*Mask = (byte)source[pos++];*/

			DataConverter.BigEndian.PutBytes(destination, pos, Pos[0]);
			pos += 4;
			DataConverter.BigEndian.PutBytes(destination, pos, Pos[1]);
			pos += 4;
			DataConverter.BigEndian.PutBytes(destination, pos, Pos[2]);
			pos += 4;

			if (Speed != null)
			{
				destination[maskPos] |= 1;
				DataConverter.BigEndian.PutBytes(destination, pos, Speed[0]);
				pos += 4;
				DataConverter.BigEndian.PutBytes(destination, pos, Speed[1]);
				pos += 4;
				DataConverter.BigEndian.PutBytes(destination, pos, Speed[2]);
				pos += 4;
			}
			if (Accel != null)
			{
				destination[maskPos] |= 2;
				DataConverter.BigEndian.PutBytes(destination, pos, Accel[0]);
				pos += 4;
				DataConverter.BigEndian.PutBytes(destination, pos, Accel[1]);
				pos += 4;
				DataConverter.BigEndian.PutBytes(destination, pos, Accel[2]);
				pos += 4;
			}
			if (DragNormal != null)
			{
				destination[maskPos] |= 4;
				DataConverter.BigEndian.PutBytes(destination, pos, DragNormal[0]);
				pos += 4;
				DataConverter.BigEndian.PutBytes(destination, pos, DragNormal[1]);
				pos += 4;
				DataConverter.BigEndian.PutBytes(destination, pos, DragNormal[2]);
				pos += 4;
			}
			if (Drag != float.NaN)
			{
				destination[maskPos] |= 8;
				DataConverter.BigEndian.PutBytes(destination, pos, Drag);
				pos += 4;
			}
        }
    }
}
